package org.example.sword2offer.primary;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author: lynn
 * @Descript: 微信抢红包算法,公平版 二分均值法 100/10， 【0，100/10)+1， 为了避免小数，统一扩大1000，用1000代表10块钱。
 * @date: 2021/6/4 7:31
 * @version: 1.0
 */
public class Q67_1_WechatGrabRed {
    public static void main(String[] args) {
        List<Integer> amoutList= divideRedPackage(1000,10);
        for(Integer amout:amoutList){
            System.out.printf("领到的金额:" + new BigDecimal(amout).divide(new BigDecimal(100)));
        }
    }
    public static List<Integer> divideRedPackage(Integer totalAmount,Integer totalPeopleNum){
        List<Integer> amountList=new ArrayList<>();
        Integer restAmount=totalAmount;Integer restPeopleNum=totalPeopleNum;
        Random random= new Random();
        for (int i = 0; i < totalPeopleNum-1 ; i++) {
            //totalPeopleNum-1 代表分了次数，比如说你5个人，就分了4次；random.nextInt 是左闭右开所以加1
            int amount=random.nextInt(restAmount/restPeopleNum * 2)+1;
            restAmount -=amount; restPeopleNum --;amountList.add(amount);
        }
        amountList.add(restAmount);
        return amountList;
    }
}
